package com.novunda.platform.services;

import com.novunda.platform.common.exception.NovException;
import com.novunda.platform.common.reponse.CommonResponse;
import com.novunda.platform.common.reponse.QrCodeStatusResponse;
import com.novunda.platform.common.utils.UserUtils;
import com.novunda.platform.enums.ValidateExceptionEnum;
import com.novunda.platform.repositories.CenterRepository;
import com.novunda.tca.tdk.domain.ApplyQrCodeResponse;
import com.novunda.tca.tdk.domain.DeviceInfo;
import com.novunda.tca.tdk.domain.DeviceInfoResponse;
import com.novunda.tca.tdk.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ..
 * 个人中心service
 *
 * @author yangg
 * @since 2016/9/9.
 */
@Service
public class CenterService extends BaseService {
    @Autowired
    private CenterRepository centerRepository;

    /**
     * 分页获取已绑定设备信息
     *
     * @param user           用户
     * @return 分页信息
     */
    public List<DeviceInfo> devices(String user) {

        if (StringUtils.isBlank(user)) {
            throw new NovException(ValidateExceptionEnum.INVALID_PARAMS);
        }

        DeviceInfoResponse deviceInfoResponse = centerRepository.devices(user, "-1");

        if (deviceInfoResponse == null || deviceInfoResponse.getReturnCode() != 0) {
            throw new NovException(deviceInfoResponse == null ? "" : deviceInfoResponse.getReturnMessage());
        }
        return deviceInfoResponse.getInfoList();
    }

    /**
     * 生成个人中心绑定二维码
     *
     * @param userName 帐号
     * @return 二维码地址
     */
    public ApplyQrCodeResponse qrCode(String userName) {

        ApplyQrCodeResponse qrCodeResponse = centerRepository.applyQrCode(userName);

        if (qrCodeResponse == null || qrCodeResponse.getReturnCode() != 0) {
            throw new NovException(qrCodeResponse == null ? "获取绑定二维码失败" : qrCodeResponse.getReturnMessage());
        }
        return qrCodeResponse;
    }

    /**
     * 获取二维码扫描状态
     *
     * @param scode 二维码code
     * @return 扫描结果
     */
    public QrCodeStatusResponse qrStatus(String scode) {
        if (StringUtils.isBlank(scode)) {
            throw new NovException(ValidateExceptionEnum.INVALID_PARAMS);
        }
        QrCodeStatusResponse qrCodeStatusResponse = centerRepository.qrStatus(scode);

        if (qrCodeStatusResponse == null) {
            throw new NovException("获取状态失败:返回对象为空" );
        }
        return qrCodeStatusResponse;
    }

    /**
     * 解除绑定
     *
     * @param deviceId 设备ID
     */
    public void release(String deviceId) {

        if (StringUtils.isBlank(deviceId)) {
            throw new NovException(ValidateExceptionEnum.INVALID_DEVICEID_NULL);
        }

        CommonResponse commonResponse = centerRepository.release(UserUtils.getUser().getLoginName(), deviceId);
        if (commonResponse == null || commonResponse.getReturnCode() != 0) {
            throw new NovException("解除绑定失败" + (commonResponse == null ? "" : commonResponse.getReturnMessage()));
        }
    }
}
